package com.huawei.appgallery.common.media.activity;

import android.app.ActionBar;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.os.Handler;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.FrameLayout;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.huawei.android.app.ActionBarEx;
import com.huawei.appgallery.common.media.adapter.BaseThumbnailAdapter;
import com.huawei.appgallery.common.media.adapter.GroupAdapter;
import com.huawei.appgallery.common.media.api.IImageBrowseProtocol;
import com.huawei.appgallery.common.media.api.IImageBrowseResult;
import com.huawei.appgallery.common.media.api.IMediaSelectProtocol;
import com.huawei.appgallery.common.media.api.IMediaSelectResult;
import com.huawei.appgallery.common.media.bean.SelectedMediaInfo;
import com.huawei.appgallery.foundation.ui.framework.activity.AbstractBaseActivity;
import com.huawei.hmf.annotation.ActivityDefine;
import java.lang.ref.WeakReference;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import o.bld;
import o.ble.c;
import o.ble.d;
import o.ble.g;
import o.ble.j;
import o.blf;
import o.bli;
import o.blj;
import o.blm;
import o.bln;
import o.cdn;
import o.cdo;
import o.cdv;
import o.ceh;
import o.PermissionChecker;
import o.cgs;
import o.cgv;
import o.dmi;
import o.dmp;
import o.dmq;
import o.ContentManager;
import o.fho;
import o.fiu;
import o.fiv;
import o.fjb;
import o.fjc;
import o.fjf;

@ActivityDefine(alias = "MediaSelectImpl", protocol = IMediaSelectProtocol.class, result = IMediaSelectResult.class)
public class MediaSelectActivity extends AbstractBaseActivity implements bli, ceh {
    private BaseThumbnailAdapter ʻॱ;
    private String ʽॱ = "image";
    private TextView ʾ;
    private View ʿ;
    private HashMap<Integer, SelectedMediaInfo> ˈ = new HashMap();
    private int ˉ = 9;
    private String[] ˊˊ;
    private String[] ˊˋ;
    private LinearLayout ˊॱ;
    private boolean ˊᐝ = false;
    private long ˋˊ = -1;
    private blj ˋॱ;
    private fiv ˋᐝ = fiv.ॱ(this);
    private Handler ˍ = new Handler();
    private ListView ˏॱ;
    private GridView ͺ;
    private RelativeLayout ॱˊ;
    private ActionBar ॱˋ;
    private GroupAdapter ॱˎ;
    private bln ᐝॱ;

    static class a implements dmi {
        private WeakReference<MediaSelectActivity> ˋ;
        private String[] ˎ;
        private String[] ˏ;
        private String ॱ;

        public a(MediaSelectActivity mediaSelectActivity, String str, String[] strArr, String[] strArr2) {
            this.ˋ = new WeakReference(mediaSelectActivity);
            this.ॱ = str;
            this.ˏ = strArr;
            this.ˎ = strArr2;
        }

        public void run() {
            bln.ˊ().ॱ(ContentManager.getInstant().getContext(), this.ॱ, this.ˏ, this.ˎ);
            MediaSelectActivity mediaSelectActivity = (MediaSelectActivity) this.ˋ.get();
            if (mediaSelectActivity != null) {
                mediaSelectActivity.ʼॱ();
            }
        }
    }

    static class e extends fjc<IImageBrowseResult> {
        private e() {
        }

        public /* synthetic */ void ˎ(int i, Object obj) {
            ˊ(i, (IImageBrowseResult) obj);
        }

        public void ˊ(int i, IImageBrowseResult iImageBrowseResult) {
            if (i == -1 && iImageBrowseResult != null) {
                ((MediaSelectActivity) ˊ()).ˎ(blm.ॱ(iImageBrowseResult.getSelectedMedias()));
            }
        }
    }

    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        getWindow().setBackgroundDrawableResource(o.ble.a.ˋ);
        if (!PermissionChecker.ˋ(this, 16)) {
            ᐝॱ();
        }
    }

    private void ͺ() {
        int integer;
        setContentView(j.ˎ);
        ˊᐝ();
        this.ᐝॱ = bln.ˊ();
        this.ˋॱ = blj.ˋ();
        this.ˊॱ = (LinearLayout) findViewById(b.ˏॱ);
        ˊˋ();
        this.ॱˊ = (RelativeLayout) findViewById(b.ˊॱ);
        this.ˏॱ = (ListView) findViewById(b.ॱˋ);
        this.ˏॱ.setOnScrollListener(new OnScrollListener(this) {
            final /* synthetic */ MediaSelectActivity ˎ;

            {
                this.ˎ = r1;
            }

            public void onScrollStateChanged(AbsListView absListView, int i) {
                if (i != 0) {
                    this.ˎ.ˋॱ.ॱ(true);
                } else {
                    this.ˎ.ˋॱ.ॱ(false);
                }
            }

            public void onScroll(AbsListView absListView, int i, int i2, int i3) {
            }
        });
        this.ͺ = (GridView) findViewById(b.ॱॱ);
        if (cdn.ˋ()) {
            integer = getResources().getInteger(o.ble.e.ˊ);
        } else {
            integer = getResources().getInteger(o.ble.e.ॱ);
        }
        this.ͺ.setNumColumns(integer);
        this.ͺ.setOnScrollListener(new OnScrollListener(this) {
            final /* synthetic */ MediaSelectActivity ˎ;

            {
                this.ˎ = r1;
            }

            public void onScrollStateChanged(AbsListView absListView, int i) {
                if (i == 2) {
                    this.ˎ.ˋॱ.ॱ(true);
                } else {
                    this.ˎ.ˋॱ.ॱ(false);
                }
            }

            public void onScroll(AbsListView absListView, int i, int i2, int i3) {
            }
        });
        ˋˊ();
        ˏ(this.ˈ.size());
    }

    private void ᐝॱ() {
        IMediaSelectProtocol iMediaSelectProtocol = (IMediaSelectProtocol) this.ˋᐝ.ॱ();
        this.ʽॱ = iMediaSelectProtocol.getMediaType();
        this.ˊˊ = iMediaSelectProtocol.getMimeTyes();
        this.ˉ = iMediaSelectProtocol.getMaxSelectSize();
        this.ˋˊ = iMediaSelectProtocol.getMaxSelectFileSize();
        List selectedImages = iMediaSelectProtocol.getSelectedImages();
        if (selectedImages != null && selectedImages.size() > 0) {
            this.ˈ = blm.ॱ(selectedImages);
        }
        this.ˊᐝ = iMediaSelectProtocol.getSelectForHeadImg();
        this.ˊˋ = iMediaSelectProtocol.getCheckFileExtendNames();
        if (this.ˊᐝ) {
            this.ˉ = 1;
        } else if (this.ˉ <= 0) {
            this.ˉ = 9;
        }
        dmq.ˏ.ˊ(dmp.CONCURRENT, new a(this, this.ʽॱ, this.ˊˋ, this.ˊˊ));
    }

    private void ʼॱ() {
        this.ˍ.post(new Runnable(this) {
            final /* synthetic */ MediaSelectActivity ˏ;

            {
                this.ˏ = r1;
            }

            public void run() {
                this.ˏ.ͺ();
            }
        });
    }

    public void onActivityResult(int i, int i2, Intent intent) {
        super.onActivityResult(i, i2, intent);
    }

    public void onConfigurationChanged(Configuration configuration) {
        int integer;
        super.onConfigurationChanged(configuration);
        if (cdn.ˋ()) {
            integer = getResources().getInteger(o.ble.e.ˊ);
        } else {
            integer = getResources().getInteger(o.ble.e.ॱ);
        }
        this.ͺ.setNumColumns(integer);
        ˊˋ();
    }

    private void ˊᐝ() {
        this.ʿ = findViewById(b.ˌ);
        this.ॱˋ = getActionBar();
        cgv.ˋ(this, o.ble.a.ˏ, o.ble.a.ˋ);
        int i = d.ˋ;
        int i2 = d.ˎ;
        if (cdv.ˋ() >= 21) {
            ActionBarEx.setStartIcon(this.ॱˋ, true, getResources().getDrawable(i), new OnClickListener(this) {
                final /* synthetic */ MediaSelectActivity ˏ;

                {
                    this.ˏ = r1;
                }

                public void onClick(View view) {
                    this.ˏ.finish();
                }
            });
            if (this.ˉ > 1) {
                ActionBarEx.setEndIcon(this.ॱˋ, true, getResources().getDrawable(i2), new OnClickListener(this) {
                    final /* synthetic */ MediaSelectActivity ˋ;

                    {
                        this.ˋ = r1;
                    }

                    public void onClick(View view) {
                        this.ˋ.ˉ();
                    }
                });
            }
        } else {
            this.ʾ = (TextView) this.ʿ.findViewById(b.ˏˎ);
            ((ImageView) this.ʿ.findViewById(b.ˏˏ)).setImageResource(i);
            findViewById(b.ᐝॱ).setOnClickListener(new OnClickListener(this) {
                final /* synthetic */ MediaSelectActivity ˊ;

                {
                    this.ˊ = r1;
                }

                public void onClick(View view) {
                    this.ˊ.finish();
                }
            });
            if (this.ˉ > 1) {
                ((ImageView) this.ʿ.findViewById(b.ˈ)).setBackgroundResource(i2);
                findViewById(b.ʽॱ).setOnClickListener(new OnClickListener(this) {
                    final /* synthetic */ MediaSelectActivity ˏ;

                    {
                        this.ˏ = r1;
                    }

                    public void onClick(View view) {
                        this.ˏ.ˉ();
                    }
                });
            }
        }
        ʼ(this.ˈ.size());
    }

    private void ˊ(HashMap<Integer, SelectedMediaInfo> hashMap) {
        this.ˈ.clear();
        this.ˈ.putAll(hashMap);
    }

    private void ˎ(HashMap<Integer, SelectedMediaInfo> hashMap) {
        if (hashMap != null) {
            ˊ((HashMap) hashMap);
            if (this.ʻॱ != null) {
                this.ʻॱ.setSelectedMap(this.ˈ);
                if (this.ˈ.size() == 1 && this.ˉ == 1) {
                    ˉ();
                    return;
                }
                this.ʻॱ.notifyDataSetChanged();
            }
            ˏ(this.ˈ.size());
        }
    }

    private void ˏ(int i) {
        ʼ(i);
    }

    private void ˉ() {
        if (this.ʻॱ != null) {
            this.ˈ.clear();
            this.ˈ.putAll(this.ʻॱ.getSelectedMediaMap());
            fjf ˊ = fjf.ˊ(this);
            ((IMediaSelectResult) ˊ.ॱ()).setSelectedMedias(blm.ˎ(this.ˈ));
            setResult(-1, ˊ.ˊ());
        }
        finish();
    }

    private void ˋˊ() {
        if (cgs.ˋ(bln.ˊ().ˎ())) {
            this.ˊॱ.setVisibility(0);
            this.ॱˊ.setVisibility(8);
            return;
        }
        this.ˊॱ.setVisibility(8);
        this.ॱˊ.setVisibility(0);
        this.ʻॱ = bld.ˏ(getApplicationContext(), this.ʽॱ);
        this.ʻॱ.setSelectMaxSize(this.ˉ);
        this.ʻॱ.insertFirstAblumItem();
        this.ʻॱ.setSelectFileMaxSize(this.ˋˊ);
        this.ʻॱ.setILoadImageListener(this);
        this.ʻॱ.setSelectedMap(this.ˈ);
        this.ʻॱ.setSelectForHeadImg(this.ˊᐝ);
        this.ͺ.setAdapter(this.ʻॱ);
        if (this.ˉ == 1) {
            ˋˋ();
            this.ˏॱ.setVisibility(0);
        }
    }

    public void onDestroy() {
        super.onDestroy();
        if (this.ᐝॱ != null) {
            this.ᐝॱ.ʽ();
        }
        if (this.ˋॱ != null) {
            this.ˋॱ.ˏ();
        }
    }

    public void ˏ(String str) {
        this.ʻॱ.refreshDateSet(str);
        ˊˊ();
        if (this.ˉ == 1) {
            ʼ(0);
        }
    }

    public void ˎ(int i) {
        ˏ(i);
    }

    private void ˊˊ() {
        float f = 1.0f;
        int i = 4;
        if (this.ˏॱ == null) {
            blf.ˋ.ˊ("MediaSelectActivity", "groupListView null");
            return;
        }
        float f2;
        if (this.ˏॱ.getVisibility() != 4) {
            f2 = 0.0f;
        } else {
            f2 = 1.0f;
            i = 0;
            f = 0.0f;
        }
        this.ˏॱ.setVisibility(i);
        Animation translateAnimation = new TranslateAnimation(1, 0.0f, 1, 0.0f, 1, f2, 1, f);
        translateAnimation.setRepeatCount(0);
        translateAnimation.setDuration(300);
        this.ˏॱ.startAnimation(translateAnimation);
    }

    private void ˊˋ() {
        int ˎ = cdo.ˎ(this);
        ˎ = (((ˎ * 3) / 10) - cdo.ˋ(this)) - getResources().getDimensionPixelSize(c.ˎ);
        LayoutParams layoutParams = new FrameLayout.LayoutParams(-1, -2);
        layoutParams.setMargins(0, ˎ, 0, 0);
        this.ˊॱ.setLayoutParams(layoutParams);
    }

    private void ʼ(int i) {
        ˎ(i, this.ॱˋ, this.ʿ, this.ʾ);
    }

    private void ˎ(int i, ActionBar actionBar, View view, TextView textView) {
        CharSequence ˋ = ˋ(this.ʽॱ, i);
        if (cdv.ˋ() >= 21) {
            if (view != null) {
                view.setVisibility(8);
            }
            if (actionBar != null) {
                actionBar.show();
                actionBar.setTitle(ˋ);
                return;
            }
            return;
        }
        if (actionBar != null) {
            actionBar.hide();
        }
        if (textView != null) {
            if (view != null) {
                view.setVisibility(0);
            }
            textView.setText(ˋ);
        }
    }

    private String ˋ(String str, int i) {
        if (this.ˉ == 1) {
            if (this.ˏॱ != null && this.ˏॱ.getVisibility() == 0 && this.ˉ == 1) {
                return getString(g.ॱ);
            }
            return getString(g.ˊ);
        } else if (i <= 0) {
            return getString(g.ˋ);
        } else {
            return getResources().getQuantityString(i.ˏ, this.ˉ, new Object[]{Integer.valueOf(i), Integer.valueOf(this.ˉ)});
        }
    }

    public void ᐝ() {
        ˋˋ();
        ˊˊ();
    }

    private void ˋˋ() {
        if (this.ॱˎ == null) {
            this.ᐝॱ.ˋ();
            this.ʻॱ.initOriginalImgBeanMap();
            this.ॱˎ = new GroupAdapter(this, this.ʽॱ);
            this.ॱˎ.setILoadImageListener(this);
            this.ˏॱ.setAdapter(this.ॱˎ);
        }
    }

    public void ॱ(int i) {
        this.ˈ.clear();
        this.ˈ.putAll(this.ʻॱ.getSelectedMediaMap());
        if ("image".equals(this.ʽॱ)) {
            fjb ˎ = fho.ˏ().ˋ("Media").ˎ("ImageBrowse");
            IImageBrowseProtocol iImageBrowseProtocol = (IImageBrowseProtocol) ˎ.ˊ();
            iImageBrowseProtocol.setMediaType("image");
            iImageBrowseProtocol.setMimeTyes(this.ˊˊ);
            iImageBrowseProtocol.setMaxSelectSize(this.ˉ);
            iImageBrowseProtocol.setMaxSelectFileSize(this.ˋˊ);
            iImageBrowseProtocol.setCheckFileExtendNames(this.ˊˋ);
            if (!this.ʻॱ.isAllGroup() || this.ˉ <= 1) {
                iImageBrowseProtocol.setBrowseStartPostion(i);
            } else {
                iImageBrowseProtocol.setBrowseStartPostion(i - 1);
            }
            iImageBrowseProtocol.setBrowseGroupName(this.ʻॱ.getCurrGroupName());
            if (this.ˈ.size() > 0) {
                iImageBrowseProtocol.setSelectedImages(blm.ˎ(this.ˈ));
            }
            fiu.ˊ().ॱ((Context) this, ˎ, new e());
        } else if ("video".equals(this.ʽॱ)) {
            Collection values = this.ˈ.values();
            if (values.size() > 0) {
                boolean z;
                SelectedMediaInfo[] selectedMediaInfoArr = (SelectedMediaInfo[]) values.toArray(new SelectedMediaInfo[values.size()]);
                Intent intent = new Intent(this, VideoPlayActivity.class);
                intent.putExtra("VIDEO_PATH", selectedMediaInfoArr[0].ॱ.ʽ());
                if (selectedMediaInfoArr[0].ॱ.ˎ() > selectedMediaInfoArr[0].ॱ.ॱ()) {
                    z = true;
                } else {
                    z = false;
                }
                intent.putExtra("VIDEO_ORIENTATION", z);
                startActivity(intent);
            }
        }
    }

    public boolean onKeyDown(int i, KeyEvent keyEvent) {
        if (i == 4) {
            if (this.ˏॱ == null || this.ˏॱ.getVisibility() != 0) {
                if (this.ʻॱ != null && (!this.ʻॱ.isAllGroup() || this.ˉ == 1)) {
                    ˊˊ();
                    if (this.ˉ != 1) {
                        return true;
                    }
                    ʼ(0);
                    return true;
                }
            } else if (this.ˉ == 1) {
                return super.onKeyDown(i, keyEvent);
            } else {
                ˏ("all_medias");
                return true;
            }
        }
        return super.onKeyDown(i, keyEvent);
    }

    public void ॱˊ() {
        ˉ();
    }

    public void onRequestPermissionsResult(int i, String[] strArr, int[] iArr) {
        PermissionChecker.ˎ(this, i, strArr, iArr);
    }

    public void ˏ(int i, int i2) {
        Object obj = null;
        if (i == 16 && i2 != -1) {
            obj = 1;
        }
        if (obj != null) {
            ᐝॱ();
        } else {
            finish();
        }
    }
}
